﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Zero.UControl.Component
{
    public class ScrollList : ContentControl
    {
        private Storyboard _storyboard;
        private FrameworkElement PART_ContentHost;
        private FrameworkElement TempBorder;
        private double _contentHeight;

        static ScrollList()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollList),
                new FrameworkPropertyMetadata(typeof(ScrollList)));
        }

        #region 依赖属性
        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(ScrollList), new PropertyMetadata(null));

        public double ScrollHeight
        {
            get { return (double)GetValue(ScrollHeightProperty); }
            set { SetValue(ScrollHeightProperty, value); }
        }

        public static readonly DependencyProperty ScrollHeightProperty =
            DependencyProperty.Register("ScrollHeight", typeof(double), typeof(ScrollList), new PropertyMetadata(240d));

        public Brush ContentBorderBrush
        {
            get { return (Brush)GetValue(ContentBorderBrushProperty); }
            set { SetValue(ContentBorderBrushProperty, value); }
        }

        public static readonly DependencyProperty ContentBorderBrushProperty =
            DependencyProperty.Register("ContentBorderBrush", typeof(Brush), typeof(ScrollList), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(61, 113, 255))));


        public bool IsScrolling
        {
            get { return (bool)GetValue(IsScrollingProperty); }
            set { SetValue(IsScrollingProperty, value); }
        }

        public static readonly DependencyProperty IsScrollingProperty =
            DependencyProperty.Register("IsScrolling", typeof(bool), typeof(ScrollList), new PropertyMetadata(true, OnScrollingPropertyChanged));

        private static void OnScrollingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var scroll = d as ScrollList;
            scroll._storyboard.Stop();
        }
        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_ContentHost = GetTemplateChild("PART_ContentHost") as FrameworkElement;
            TempBorder = GetTemplateChild("TempBorder") as Border;
            if (PART_ContentHost != null)
            {
                // 确保每次重新应用模板时先清除旧事件
                PART_ContentHost.Loaded -= PART_ContentHost_Loaded;
                PART_ContentHost.Loaded += PART_ContentHost_Loaded;
            }

            if (TempBorder != null)
            {
                TempBorder.MouseEnter -= TempRoot_MouseEnter;
                TempBorder.MouseLeave -= TempRoot_MouseLeave;
                TempBorder.MouseEnter += TempRoot_MouseEnter;
                TempBorder.MouseLeave += TempRoot_MouseLeave;
            }
        }

        private void TempRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            _storyboard.Pause(); // 暂停动画
        }

        private void TempRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            _storyboard.Resume(); // 继续动画
        }

        private void PART_ContentHost_Loaded(object sender, RoutedEventArgs e)
        {
            // 内容加载完成后再开启动画
            Dispatcher.BeginInvoke(new Action(() =>
            {
                DoAnimation();
            }), System.Windows.Threading.DispatcherPriority.Render);
        }

        private void DoAnimation()
        {
            _contentHeight = PART_ContentHost.ActualHeight - ScrollHeight;

            var animation = new DoubleAnimationUsingKeyFrames()
            {
                RepeatBehavior = RepeatBehavior.Forever
            };

            animation.KeyFrames.Add(new DiscreteDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))));
            animation.KeyFrames.Add(new LinearDoubleKeyFrame(-_contentHeight, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6))));
            animation.KeyFrames.Add(new DiscreteDoubleKeyFrame(-_contentHeight, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(7))));
            animation.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(13))));
            animation.KeyFrames.Add(new DiscreteDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(14))));

            Storyboard.SetTarget(animation, PART_ContentHost);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));

            _storyboard = new Storyboard();
            _storyboard.Children.Add(animation);
            _storyboard.Begin();
        }
    }
}
